// pages/add/index.js
const app = getApp()
const defaultEvent = {
    eTitle: "",
    eTime: "",
    iuId: "",
    eLocation: "",
    eType: "",
    eInfo: "",
    eEvaluation: "",
    eContact: ""
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTypeName: "建议",
        eventTypes: [{
            value: 1,
            label: '建议',
        }, {
            value: 2,
            label: '投诉'
        }, {
            value: 3,
            label: '反馈'
        }],
        eventDetail: defaultEvent
    },
    handleEventTypeChange({ detail = {} }) {
        console.log(detail)
        this.setData({
            "currentTypeName": detail.value
        });
    },
    hanleETitleChange({ detail = {} }) {
        this.setData({
            "eventDetail.eTitle": detail.detail.value
        })
    },
    hanleETimeChange({ detail = {} }) {
        this.setData({
            "eventDetail.eTime": detail.detail.value
        })
    },
    hanleELocationChange({ detail = {} }) {
        this.setData({
            "eventDetail.eLocation": detail.detail.value
        })
    },
    hanleEInfoChange({ detail = {} }) {
        this.setData({
            "eventDetail.eInfo": detail.detail.value
        })
    },
    hanleEContactChange({ detail = {} }) {
        this.setData({
            "eventDetail.eContact": detail.detail.value
        })
    },
    submitEvent() {
        let that = this
        const type = that.data.eventTypes.filter(e => e.label === that.data.currentTypeName)
        if (type) {
            that.data.eventDetail.eType = type[0].value
        }
        const userInfo = app.globalData.userInfo
        that.data.eventDetail.iuId = userInfo.iuId
        wx.request({
            url: 'http://localhost:8080/system/wechat/event/save',
            method: 'POST',
            data: that.data.eventDetail,
            dataType: 'json',
            success: res => {
                console.log('res: ' + res)
                wx.navigateBack({
                    delta: 1
                })
            }
        })
    },
    resetEvent() {
        this.setData({
            eventDetail: defaultEvent
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.setNavigationBarTitle({
            title: "事件上报"
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})